
package org.varan.emulator;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class RemoteContorlServer {
	
	private static final int PORT =  42620;
	
	public static void main(String[] args) {
		
		ServerSocket server = null;		
		try {
			server = new ServerSocket(PORT);			
		} catch (IOException e) {
			System.out.println("Could not listen on port " + PORT);
			System.exit(-1);
		}
		
		Socket socket = null;
		try {
			socket = server.accept();
		} catch (IOException ex) {
			ex.printStackTrace();
			System.exit(-1);
		}
		
		InputStream in = null;
		try {
			StringBuilder buffer = new StringBuilder();
			in = socket.getInputStream();
			int ch = 0;
			while ((ch = in.read()) != -1) {
				buffer.append((char) ch);
			}
			System.out.println("Received message: " + buffer.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		close(in);
	}
	
	private static void close(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
